package top.huiwow.listeners;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.Vector;
import top.huiwow.GameCore;
import top.huiwow.utils.MathUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class EntityListener implements Listener {
    public static Map<Integer, List<ItemStack>> dropMap = new HashMap<>();
    GameCore plugin;

    public EntityListener() {
        plugin = GameCore.getInstance();
    }

    public static boolean isEntity(Entity entity, int internalID) {
        return entity instanceof LivingEntity && entity.hasMetadata("internalID") && entity.getMetadata("internalID").get(0).asInt() == internalID;
    }

    @EventHandler
    public void onHit(EntityDamageByEntityEvent e) {
        Entity entity = e.getEntity();
        if (entity.hasMetadata("internalID")) {
            updateNameTag((LivingEntity) entity);
            if (entity.getMetadata("nokb").get(0).asBoolean()) {
                Bukkit.getScheduler().runTaskLater(plugin, new BukkitRunnable() {
                    @Override
                    public void run() {
                        entity.setVelocity(new Vector(0, 0, 0));
                    }
                }, 1);
            }
        }
    }

    @EventHandler
    public void onRegainHP(EntityRegainHealthEvent e) {
        Entity entity = e.getEntity();
        if (entity.hasMetadata("internalID")) {
            updateNameTag((LivingEntity) entity);
        }
        if (entity instanceof Player) {
            if (e.getRegainReason().equals(EntityRegainHealthEvent.RegainReason.SATIATED)) {
                e.setAmount((((Player) entity).getMaxHealth() * 0.01) + 1);
            }
        }
    }

    public void updateNameTag(LivingEntity entity) {
        Bukkit.getScheduler().runTaskLater(plugin, new BukkitRunnable() {
            @Override
            public void run() {
                int level = entity.getMetadata("level").get(0).asInt();
                int maxHealth = entity.getMetadata("max_health").get(0).asInt();
                double health = entity.getHealth();
                String name = entity.getMetadata("name").get(0).asString();
                entity.setCustomName(ChatColor.GRAY + "LV " + level + ChatColor.RESET + "   " + ChatColor.RED + ChatColor.BOLD + name + "  " + ChatColor.RESET + " " + (health <= 0.5 * maxHealth ? (health <= 0.2 * maxHealth ? ChatColor.RED : ChatColor.YELLOW) : ChatColor.GREEN) + (MathUtils.simplifyBigNumber(health, true)) + ChatColor.RESET + "/" + ChatColor.GREEN + MathUtils.simplifyBigNumber(maxHealth, true) + ChatColor.RED + ChatColor.RED + ChatColor.BOLD + "❤");
            }
        }, 1);
    }

    @EventHandler
    public void onEvent(EntityChangeBlockEvent e) {
        if (e.getEntity() instanceof WitherSkull || e.getEntity() instanceof Wither) {
            e.setCancelled(true);
        }
    }

    @EventHandler
    public void onEntityDeath(EntityDeathEvent e) {
        if (e.getEntity().hasMetadata("internalID")) {
            Integer internalID = e.getEntity().getMetadata("internalID").get(0).asInt();
            e.getDrops().clear();
            if (dropMap.get(internalID) != null) {
                for (ItemStack is : dropMap.get(internalID)) {
                    e.getDrops().add(is);
                }
            }
        }
    }

}
